export default {
  methods: {
    // 结算
    settlement() {
      let amount = 0;
      let stage = 0;
      const orders = [];
      this.initialData.forEach((item) => {
        if (item.checked && !item.disabled) {
          amount += Number(item.amount);
          stage += 1;
          orders.push(item.order_id);
        }
      });

      this.$emit('settlement', {
        amount,
        stage,
        orders
      });
    },

    // 选择
    handlerChecked(index, checked) {
      let startIndex = 0;
      let endIndex = 0;

      if (checked) {
        endIndex = index;
      } else {
        startIndex = index;
        endIndex = (this.initialData.length - 1);
      }

      this.rangeSelect(startIndex, endIndex, checked);
    },

    // 范围选择
    rangeSelect(start, end, checked) {
      for (let i = start; i <= end; i++) {
        const item = this.initialData[i];
        if (!item.disabled) {
          this.$set(this.initialData[i], 'checked', checked);
        }
      }

      this.settlement();
    },

    // 全部选择
    selectAll(checked = true) {
      this.initialData.forEach((item) => {
        if (!item.disabled) {
          this.$set(item, 'checked', checked);
        }
      });

      this.settlement();
    }
  }
};
